<template>
  <div>
    <div class="topNav">
      <van-nav-bar
        title="地址列表"
        left-text="返回"
        left-arrow
        @click-left="quit"
      />
    </div>
    <div class="address">
      <van-address-list
        v-model="chosenAddressId"
        :list="list"
        default-tag-text="默认"
        @add="onAdd"
        @edit="onEdit"
        :switchable='false'
      />
    </div>
  </div>
</template>

<script>
export default {
  name: "Address",
  data() {
    return {
      chosenAddressId: "1",
      list: [],
      
    };
  },
  methods: {
    // 返回个人主页
    quit() {
      this.$router.go(-1);
    },
    // 添加地址
    onAdd() {
      this.$router.push({ name: "NewAddress" });
    },
    // 修改地址
    onEdit(item) {
      this.$router.push({ name: "NewAddress" ,params: {aid: item.aid}});
    },
    // 获取地址
    getAddress(){
      let token = this.$cookies.get('TOKEN')

      this.axios({
        method: "GET",
        url:'/findAddress',
        params: {
          tokenString: token,
        }
      })
        .then((res) => {
          
          if (res.data.code == 700) {
            //用户未登录, 拦截操作, 通知用户去登录
            this.$toast('请先登录');
            setTimeout(() => {
              this.$router.push({name: 'Login'});
            }, 2000)
            return
          }
          
          if(res.data.code === 20000){
            res.data.result.forEach(v => {
              v.isDefault = Boolean(v.isDefault);
              v.address = `${v.province}${v.city}${v.county}${v.addressDetail}`
            });
          }
          this.list = res.data.result;
          
        })
        .catch((err) => {
          
        });
    }
  },
  created(){
    this.getAddress()
  }
};
</script>

<style lang="less" scoped>
.topNav {
  position: sticky;
  top: 0;
  /deep/ .van-nav-bar__text {
    color: #0c34ba;
  }
  /deep/ .van-nav-bar .van-icon {
    color: #0c34ba;
  }
}
.address {
  /deep/ .van-address-item .van-radio__icon--checked .van-icon {
    background-color: #0c34ba;
    border-color: #0c34ba;
  }
  /deep/ .van-tag--danger {
    background-color: #0c34ba;
  }
  /deep/ .van-button--danger {
    background-color: #0c34ba;
    border-color: #0c34ba;
  }
}
</style>